package domain;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import com.aar.rest.util.HibernateUtil;

public class GiftDAO implements IGiftDAO{
	
	public List<Gift> getAllGifts() {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	    Session session = sessionFactory.openSession();
	    @SuppressWarnings("unchecked")
		List<Gift> gifts = session.createCriteria(Gift.class).addOrder(Order.asc("id")).list();
	    return gifts;
	}

	public void addGift(ArrayList<String> gifts2, long idChild) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
		Session session = sessionFactory.openSession();
		org.hibernate.Transaction tx = session.beginTransaction();   
		for(int i=0;i<gifts2.size();i++){
			if(!gifts2.get(i).equals("")){
				Gift gift = new Gift();
				gift.setName(gifts2.get(i));
				Child child = (Child) session.load(Child.class, idChild);
				gift.setChild(child);
				child.getGifts().add(gift);	
				session.save(child);
			}
		}
		tx.commit(); 	
	}

	public Gift getGift(long id) {
		 SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
         Session session = sessionFactory.openSession();
         org.hibernate.Transaction tx = session.beginTransaction();
         Gift gift = (Gift) session.load(Gift.class, id);
         tx.commit();
         return gift;
	}

	@Override
	public boolean deleteGift(long id) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        Gift gift = (Gift) session.get(Gift.class, id);
        if (gift == null)
        	return false;
        session.delete(gift);
        tx.commit();
        return true;
	}


	public List<Gift> getGiftsByChild(long idChild) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        @SuppressWarnings("unchecked")
		List<Gift> gifts = session.createQuery("from Gift where ID_Child = "+idChild).list(); 
        return gifts;
	}

	public Gift getGiftByChild(long idChild, long idGift) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        @SuppressWarnings("unchecked")
		List<Gift> gifts = session.createQuery("from Gift where ID_Child = " + idChild + " and ID_Gift = " + idGift).list(); 
        return gifts.get(0);
	}
	
	@Override
	public boolean updateGift(long id, String name, boolean isValid, boolean isCreate) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        Gift gift = (Gift) session.get(Gift.class, id);
        if(gift == null){
        	return false;
        }
    	gift.setName(name);
		gift.setIsValid(isValid);
		gift.setIsCreate(isCreate);
        session.save(gift);
        tx.commit();
        return true;
	}
	
	@Override
	public boolean updateGiftCreate(long id, boolean isCreate) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        Gift gift = (Gift) session.get(Gift.class, id);
        if(gift == null){
        	return false;
        }
		gift.setIsCreate(isCreate);
        session.save(gift);
        tx.commit();
        return true;
	}

	@SuppressWarnings("unchecked")
	public List<Gift> getGiftByAll(String name, String isValide, String isCreate) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        List<Gift> gifts = null; 
        String sql = myRequestGift(name, isValide, isCreate);
        gifts = session.createQuery(sql).list();
        return gifts;
	}
	
	public String myRequestGift(String name, String isValide, String isCreate){
		String sql = "from Gift where ";
		if(!name.equals("")){
        	sql += "Name ='"+ name + "' ";
        	if(!isValide.equals("")){
        		if(isValide.equals("oui")){
    				sql += "and IsValid = 1 ";
    				if(!isCreate.equals("")){
            			if(isCreate.equals("oui")){
            				sql += "and IsCreate = 1 ";
                    	}else{
                    		sql += "and IsCreate = 0 ";
                    	}	       			
            		}
            	}else{
            		sql += "and IsValid = 0 ";
            		if(!isCreate.equals("")){
            			if(isCreate.equals("oui")){
            				sql += "and IsCreate = 1 ";
                    	}else{
                    		sql += "and IsCreate = 0 ";
                    	}	       			
            		}
            	}	  
        	}else{
        		if(!isCreate.equals("")){
        			if(isCreate.equals("oui")){
        				sql += "and IsCreate = 1 ";
                	}else{
                		sql += "and IsCreate = 0 ";
                	}	       			
        		}
        	}
        }else{
        	if(!isValide.equals("")){
        		if(isValide.equals("oui")){
    				sql += "IsValid = 1 ";
    				if(!isCreate.equals("")){
            			if(isCreate.equals("oui")){
            				sql += "and IsCreate = 1 ";
                    	}else{
                    		sql += "and IsCreate = 0 ";
                    	}	       			
            		}
            	}else{
            		sql += "IsValid = 0 ";
            		if(!isCreate.equals("")){
            			if(isCreate.equals("oui")){
            				sql += "and IsCreate = 1 ";
                    	}else{
                    		sql += "and IsCreate = 0 ";
                    	}	       			
            		}
            	}	  
        	}else{
        		if(!isCreate.equals("")){
        			if(isCreate.equals("oui")){
        				sql += "IsCreate = 1 ";
                	}else{
                		sql += "IsCreate = 0 ";
                	}	       			
        		}
        	}
        }
		return sql;
	}

	public boolean updateGiftValid(long id, boolean isValid) {
		SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        Gift gift = (Gift) session.get(Gift.class, id);
        if(gift == null){
        	return false;
        }
		gift.setIsValid(isValid);
        session.save(gift);
        tx.commit();
        return true;		
	}
}
